import tracker from '../utils/tracker';
import onload from '../utils/onload';
export function blankScreen() {
    let wrapperElements = ['html', 'body', '#app']; // 包裹元素(也就是 不是我们要渲染的元素)
    let emptyPoints = 0; //记录空白点的次数 最多总的有18个点

    function getSelector(element) {
        if (element.id) {
            return "#" + element.id;
        } else {
            return element.nodeName.toLowerCase();
        }
    }

    function isWrapper(element) {
        let selector = getSelector(element);
        //selector如果取到的是wrapperElements里的值，说明该值是空白点
        console.log('selector', selector)
        console.log('wrapperElements.indexOf(selector) != -1', wrapperElements.indexOf(selector) != -1)
        if (wrapperElements.indexOf(selector) != -1) {
            emptyPoints++;
        }
    }
    onload(function() {
        for (let i = 1; i <= 9; i++) {
            //elementsFromPoint:该函数返还在特定坐标点下的HTML元素数组。
            /**
             * window.innerHeight / 2: 屏幕高度的一半(x轴)
             * window.innerWidth * i / 10:每个点的x轴的位置(x轴)
             * window.innerWidth / 2:  屏幕高度的一半(y轴)
             * window.innerHeight * i / 10:每个点的y轴的位置(y轴)
             */
            let xElements = document.elementsFromPoint(
                window.innerWidth * i / 10, window.innerHeight / 2);
            let yElements = document.elementsFromPoint(
                window.innerWidth / 2, window.innerHeight * i / 10, window.innerHeight / 2);
            console.log('xElements', xElements) //xElements [html]
            console.log('yElements', yElements) //yElements [div.content1, div#container, body, html]
            isWrapper(xElements[0]);
            isWrapper(yElements[0]);
        }

        if (emptyPoints >= 18) {
            let centerElements = document.elementsFromPoint(
                window.innerWidth / 2, window.innerHeight / 2
            );
            tracker.send({
                kind: 'stability',
                type: 'blank',
                emptyPoints,
                screen: window.screen.width + "X" + window.screen.height,
                viewPoint: window.innerWidth + "X" + window.innerHeight, // 适口大小
                selector: getSelector(centerElements[0])
            });
        }
    });

}